package com.dingding.special.controller.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @ClassName: ApiInvokeUrlInterceptor 
 * @Description: 授权API调用请求拦截器
 * 
 * URL请求类型分为“授权api”和“开发api”
 * 授权api需要accesstoken	
 * 开放api不需要accesstoken
 * 
 * @author CHEHAUNBO 
 * @date 2016年6月3日 下午10:31:27
 * 
 */
public class ApiInvokeUrlInterceptor implements HandlerInterceptor {

	private static final Logger log = Logger.getLogger(OpenApiInvokeUrlInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		request.setCharacterEncoding("UTF-8");

		// 客户端请求的URL
		String url = request.getRequestURL().toString();

		// 获取时间戳
		String timeStamp = request.getParameter("timestamp");
		// 当前用户授权秘钥
		String accesstoken = request.getParameter("accesstoken");
		// URl签名
		String signature = request.getParameter("signature");

		log.info("客户端请求URL:" + url + "?timeStamp=" + timeStamp + "&accesstoken=" + accesstoken + "&signature"
				+ signature);

		// return URLValidate.validate(response, url, timeStamp, accesstoken,
		// signature);
		return true;

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

		// 设置响应文本类型
		response.setContentType("application/json");
		// 设置响应编码格式
		// response.setCharacterEncoding("UTF-8");

	}

}
